package de.islanders.persistence;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import de.islanders.unit.player.Disease;
import de.islanders.unit.player.Player;
import de.islanders.unit.player.PlayerStatus;

public class DiseaseDAO {
	
	private EntityManager entityManager;
	
	public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    protected EntityManager getEntityManager() {
        if (entityManager == null)
            throw new IllegalStateException(
                    "EntityManager has not been set on DAO before usage");
        return entityManager;
    }
    
    public Disease findById(Integer id){
    	return entityManager.find(Disease.class, id);
    }
    
    @SuppressWarnings("unchecked")
	public List<Disease> findDiseases(PlayerStatus status){
    	Query query = entityManager.createQuery("SELECT d FROM Disease d WHERE playerStatus = :playerStatus");
    	query.setParameter("playerStatus", status);
    	return query.getResultList();
    }
    
    public void makePersistent(Disease disease){
    	entityManager.getTransaction().begin();
    	entityManager.persist(disease);
    	entityManager.getTransaction().commit();
    }
}
